<?php
/**
 * Created by PhpStorm.
 * User: 张彪
 * Date: 15-5-7
 * Time: 下午5:41
 */
namespace Admin\Controller;
class AppController extends AdminController {

    public function index()
    {
        $list   = $this->lists('Explain');
        $this->assign('_list', $list);
        $this->meta_title = '公告管理';
        $this->display();
    }

    public function add($title  = '', $content = '', $from = ''){
        if(IS_POST){

            $explain = array('title' => $title,  'content' => $content , 'from' => $from ,'create_time' => time());
            if(!M('Explain')->add($explain)){
                $this->error('添加失败！');
            } else {
                $this->success('添加成功！',U('index'));
            }
        } else {
            $this->meta_title = '新增公告';
            $this->display();
        }
    }

    public function delete($id)
    {
        if(!M('Explain')->delete($id)){
            $this->error('删除失败！');
        } else {
            $this->success('删除成功！');
        }
    }
    public function edit(){
        $id = I('get.id','');
        if(empty($id)){
            $this->error('参数不能为空！');
        }

        /*获取一条记录的详细数据*/
        $Model = M('Explain');
        $data = $Model->field(true)->find($id);
        if(!$data){
            $this->error($Model->getError());
        }

        $this->assign('info', $data);
        $this->meta_title = '编辑公告';
        $this->display();
    }

    public function update(){
        $res = M('Explain')->save($_POST);

        if(!$res){
            $this->error('未发现变化！');;
        }else{
            $this->success('更新成功',U('index'));
        }
    }

}